package admin

import (
	"github.com/gin-gonic/gin"
	"gostudy/pkg/captcha/store"
	services "gostudy/system/services/admin"
	"net/http"
)

type LoginController struct {
	homeController
}

func NewLoginController() LoginController {
	return LoginController{}
}

func (con LoginController) Routes(rg *gin.RouterGroup) {

	/*******登录路由**********/
	rg.GET("/login", con.login)
	rg.POST("/login", con.login)

}
func (con LoginController) login(c *gin.Context) {

	if c.Request.Method == "GET" {
		con.Html(c, http.StatusOK, "login.html", gin.H{
			"title": "沧澜管理平台",
		})
	} else {
		username := c.PostForm("username")
		//password := c.PostForm("password")

		// 为测试方便release模式才开启验证码
		if gin.Mode() == gin.ReleaseMode {

			captch := c.PostForm("captcha")
			var store = store.NewSessionStore(c, 20)
			verify := store.Verify("", captch, true)
			if !verify {
				con.Error(c, "验证码错误")
				return
			}
		}

		adminUser, err := services.NewAdminUserService().GetAdminUser(map[string]interface{}{"username": username})
		if err != nil {
			con.Error(c, "账号密码错误")
			return
		}

		println(adminUser.Username)
		////判断密码是否正确
		//if gstrings.Encryption(password, adminUser.Salt) == adminUser.Password {
		//
		//	userInfo := make(map[string]interface{})
		//	userInfo["username"] = adminUser.Username
		//	//session 存储数据
		//	session := sessions.Default(c)
		//	userstr, _ := json.Marshal(userInfo)
		//
		//	session.Set("userInfo", string(userstr))
		//	session.Save()
		//
		//	con.Success(c, "/admin/home", "登录成功")
		//} else {
		//	con.Error(c, "账号密码错误")
		//}

	}
}
